<?php

$version="1.0";

if(file_exists("../config.php"))
	{
	header("Location:../index.php");
	exit();
	}

if(isset($_POST['action']))
	$action = $_POST['action'];

if((isset($_POST['check'])) && ($_POST['check'] == " Check DB Connection "))
	{
	unset($dblogin);
	if(($_POST['db_host'] == "") || ($_POST['db_port'] == "") || ($_POST['db_name'] == "") || ($_POST['db_user'] == ""))
		$dblogin = 0; //some fields are left blank
	else 
		{
		$link = @mysql_connect($_POST['db_host'].":".$_POST['db_port'], $_POST['db_user'], $_POST['db_pass']);

		if(!$link) // can't connect to database
			$dblogin = 1;
		else 
			{
			$db_selected = mysql_select_db($_POST['db_name'], $link);

			if(!$db_selected) //can't switch to mentioned database
				$dblogin = 2;
			else // connection successfull and database exists
				$dblogin = 3;
			}
		}
	}
else if((isset($_POST['check'])) && ($_POST['check'] == " Create DB Tables "))
	{
	$link = mysql_connect($_POST['db_host'].":".$_POST['db_port'], $_POST['db_user'], $_POST['db_pass']);

	function TableExists($tablename, $db)
		{
   
		$result = mysql_list_tables($db);
		$rcount = mysql_num_rows($result);

		for ($i=0;$i<$rcount;$i++)
			{
			if(mysql_tablename($result, $i)==$tablename)
				return true;
			}
		return false;
		}

	$tbl_logged_exists = TableExists($_POST['tbl_logged'], $_POST['db_name']);
	if($tbl_logged_exists == "true")
		$tbl_logged_created = 0;
	else 
		{
		$create_tbl = mysql_query("CREATE TABLE `".$_POST['tbl_logged']."` 
		  (`id` int(10) NOT NULL auto_increment,
		  `player_steam` varchar(50) NOT NULL default '0',
		  `player_ip` varchar(100) default NULL,
		  `player_name` varchar(100) NOT NULL default 'Unknown',
		  `exploit_name` varchar(200) NOT NULL default '',
		  `exploit_action` varchar(8) NOT NULL default 'Allowed',
		  `time` timestamp NOT NULL default CURRENT_TIMESTAMP,
		  `server_ip` varchar(100) NOT NULL default '',
		  PRIMARY KEY  (`id`))
		  ") or die (mysql_error());
		
		$tbl_logged_created = 1;
		}

	$tbl_servers_exists = TableExists($_POST['tbl_servers'], $_POST['db_name']);
	if($tbl_servers_exists == "true")
		$tbl_servers_created = 0;
	else 
		{
		$create_tbl = mysql_query("CREATE TABLE `".$_POST['tbl_servers']."` 
		(`id` int(11) NOT NULL auto_increment,
		  `ip` varchar(32) NOT NULL,
		  `name` varchar(256) NOT NULL,
		  `lastcontact` timestamp NOT NULL default CURRENT_TIMESTAMP,
		  `version` varchar(16) NOT NULL,
		  `build_num` varchar(16) NOT NULL,
		  `build_date` varchar(16) NOT NULL,
		  `ini_version` varchar(16) NOT NULL,
		  `amxx_version` varchar(16) NOT NULL,
		  `meta_version` varchar(16) NOT NULL,
		  PRIMARY KEY  (`id`)
		)") or die (mysql_error());

		$tbl_servers_created = 1;
		}
}
else if((isset($_POST['check'])) && ($_POST['check'] == " Write Config File "))
	{
	$config->version		= $version;

	$config->db_host		= $_POST['db_host'];
	$config->db_port		= $_POST['db_port'];
	$config->db_name		= $_POST['db_name'];
	$config->db_user		= $_POST['db_user'];
	$config->db_pass		= $_POST['db_pass'];

	$config->tbl_logged		= $_POST['tbl_logged'];
	$config->tbl_servers	= $_POST['tbl_servers'];
	
	$config->display_limit	= $_POST['display_limit'];
	$config->time_diff		= $_POST['time_diff'];

	$link		= @mysql_connect($config->db_host.":".$config->db_port, $config->db_user, $config->db_pass);
	$db_selected	= @mysql_select_db($config->db_name, $link);

	$arr	= get_object_vars($config);
	$fp	= fopen("../config.php","w");

	if(fopen("../config.php","w"))
		{
		$config_fail = 0;
		
		fwrite($fp,"<?php\n\n");

		while(list($prop,$val) = each($arr))
			fwrite($fp,"$$prop = \"$val\";\n");
		
		fwrite($fp,"\n?>");
		
		fclose($fp);
		}
	else
		$config_fail = 1;
	}

?>

<html>
<head>
<? 
	echo "<title>DoD Block Exploits Web Front-End v".$version." - Installation</title>"
?>

<meta http-equiv="pragma" content="no-cache" />
<meta http-equiv="cache-control" content="no-cache" />
<meta name=\"description\" content=\"DoD Block Exploits Install\">
<meta name=\"author\" content=\"diamond-optic (diamond-optic@avamods.com)\">
<meta name=\"Abstract\" content=\"http://www.avamods.com\">
<meta name=\"Designer\" content=\"diamond-optic\">

<link rel=\"shortcut-icon\" href=\"../favicon.ico\" type=\"image/x-icon\">

<link rel="stylesheet" type="text/css" href="../css/blockexploits.css" />

</head>
<body>

<center>

<table class='install_border' border='0' cellpadding='0' cellspacing='0' width='100%'>
<tr><td class='install_border'>

<br><br><br>

<table class='install_main' border='0' cellpadding='0' cellspacing='0' width='65%'>
  <tr>
    <td width='100%' valign='top' align='center' style='padding: 20px'>
    
    <img src='../images/blockexploits_logo.png' alt='DoD Block Exploits'>

<? if ((isset($action)) && $action == " Continue To Step 2 ") { ?>

<table cellspacing='1' class='install_main' width='100%'>
	<tr>
<? 
	echo "<td colspan=\"2\" class=\"install_top\"><b>DoD Block Exploits Web Front-End v".$version." Setup - Step 2: Create Database Tables</b></td>"
?>
	</tr>
	<tr bgcolor="#7a96c8">
		<td colspan='2' class='install_1'><br>

			Here you can define names for your tables. 
<br><br>		Please note that if you change the names for any of the tables, you need to specify the same names in the DoD Block Exploits SQL table name CVARs.
<br><br>		Existing tables will <b>*not*</b> be overwritten.

		<br><br></td>
	</tr>

	<form name="section" method="post" action="<?=$_SERVER['PHP_SELF'] ?>">
	<input type="hidden" name="action" value="<?=$_POST['action'] ?>">
	<input type="hidden" name="db_host" value="<?=$_POST['db_host'] ?>">
	<input type="hidden" name="db_port" value="<?=$_POST['db_port'] ?>">
	<input type="hidden" name="db_name" value="<?=$_POST['db_name'] ?>">
	<input type="hidden" name="db_user" value="<?=$_POST['db_user'] ?>">
	<input type="hidden" name="db_pass" value="<?=$_POST['db_pass'] ?>">
	<input type="hidden" name="tbl_logged" value="<?=isset($_POST['tbl_logged']) ? $_POST['tbl_logged'] : "" ?>">
	<input type="hidden" name="tbl_servers" value="<?=isset($_POST['tbl_servers']) ? $_POST['tbl_servers'] : "" ?>">
	
	</tr>
	<tr bgcolor="#7a96c8">
		<td width='30%' class='install_1'>Logged Detections Table</td>
		<td width='70%' class='install_1'><? if ((!isset($_POST['tbl_logged'])) && (!isset($tbl_logged_created))) { ?><input type="text" name="tbl_logged" value="<? if (!isset($POST['tbl_logged'])) { echo "exp_logged"; } else { print $_POST['tbl_logged']; } ?>" style="font-family: verdana, tahoma, arial; font-size: 10px; width: 150px"> <? } else { if ($tbl_logged_created == 0) { echo "Table '".$_POST['tbl_logged']."' Exists! Skipped Creation..."; } else { echo "<font color='#20578d'>Table '".$_POST['tbl_logged']."' Successfully Created</font>"; } }?></td>
	</tr>
	<tr bgcolor="#7a96c8">
		<td width='30%' class='install_1'>Servers Table</td>
		<td width='70%' class='install_1'><? if ((!isset($_POST['tbl_servers'])) && (!isset($tbl_servers_created))) { ?><input type="text" name="tbl_servers" value="<? if (!isset($POST['tbl_servers'])) { echo "exp_servers"; } else { print $_POST['tbl_servers']; } ?>" style="font-family: verdana, tahoma, arial; font-size: 10px; width: 150px"> <? } else { if ($tbl_servers_created == 0) { echo "Table '".$_POST['tbl_servers']."' Exists! Skipped Creation..."; } else { echo "<font color='#20578d'>Table '".$_POST['tbl_servers']."' Successfully Created</font>"; } }?></td>
	</tr>

	<tr bgcolor="#7a96c8">
		<td class='install_1' colspan='2' align='right'>

		<? 
			if(isset($_POST['check']) && $_POST['check'] == " Create DB Tables ")
				echo "<font color=\"#20578d\">Database Tables Successfully Setup</font>";	
		
			
			if(isset($_POST['check']) && $_POST['check'] == " Create DB Tables ")
				{ 
				?>
				<input type='submit' name='action' value=' Continue To Final Step ' style='font-family: verdana, tahoma, arial; font-size: 10px'>
				<?
				}
			else
				{
				?>
				<input type='submit' name='check' value=' Create DB Tables ' style='font-family: verdana, tahoma, arial; font-size: 10px'>
				<? 
				}
				
		?>
				
		</td>
	</tr>
	</form>
</table>

<? } else if ((isset($action)) && ($action == " Continue To Final Step ")) { ?>

<table cellspacing='1' class='install_main' width='100%'>
	<tr>
<? 
	echo "<td colspan=\"2\" class=\"install_top\"><b>DoD Block Exploits Web Front-End v".$version." Setup - Final Step: Save Config File</b></td>"
?>
	</tr>
	<tr bgcolor="#7a96c8">
		<td colspan='2' class='install_1'><br>

		The file <b>config.php</b> will now be created.
<br><br>	The home page display limit is the total number of banned detections you wish to have displayed on the main page.
<br><br>	The time difference is for when your web server is in a different time zone then the game server.
<br>		If your web server is 1 hour behind your game server, put in 1.. If its 1 hour ahead, put in -1.. etc..
<br><br>	* After creating the config file *
<br>		You can edit all options directly in the config.php file if you wish.
<br>		You must delete the config.php file before you can run this install script again.

		<br><br></td>
	</tr>

	<form name="section" method="post" action="<?=$_SERVER['PHP_SELF'] ?>">
	<input type="hidden" name="action" value="<?=$_POST['action'] ?>">
	<input type="hidden" name="db_host" value="<?=$_POST['db_host'] ?>">
	<input type="hidden" name="db_port" value="<?=$_POST['db_port'] ?>">
	<input type="hidden" name="db_name" value="<?=$_POST['db_name'] ?>">
	<input type="hidden" name="db_user" value="<?=$_POST['db_user'] ?>">
	<input type="hidden" name="db_pass" value="<?=$_POST['db_pass'] ?>">
	<input type="hidden" name="display_limit" value="<?=$_POST['display_limit'] ?>">
	<input type="hidden" name="time_diff" value="<?=$_POST['time_diff'] ?>">
	<input type="hidden" name="tbl_logged" value="<?=isset($_POST['tbl_logged']) ? $_POST['tbl_logged'] : "" ?>">
	<input type="hidden" name="tbl_servers" value="<?=isset($_POST['tbl_servers']) ? $_POST['tbl_servers'] : "" ?>">

	<tr bgcolor="#7a96c8">
		<td width='30%' class='install_1'>Home Page Display Limit</td>
		<td width='70%' class='install_1'><input type="text" name="display_limit" value="25" style="font-family: verdana, tahoma, arial; font-size: 10px; width: 150px"></td>
	</tr>
	<tr bgcolor="#7a96c8">
		<td width='30%' class='install_1'>Time Difference</td>
		<td width='70%' class='install_1'><input type="text" name="time_diff" value="0" style="font-family: verdana, tahoma, arial; font-size: 10px; width: 150px"></td>
	</tr>

	<form name="section" method="post" action="<?=$_SERVER['PHP_SELF'] ?>">
	<input type="hidden" name="action" value="<?=$_POST['action'] ?>">
	<tr bgcolor="#7a96c8">
		<td class='install_1' colspan='2' align='right'>
<?

	if ((isset($_POST['check'])) && ($_POST['check'] == " Write Config File "))
		{
		if ($config_fail == 1)
			echo "<font color=\"#fffc00\">Failed (Enable write permission on the directory)</font>";
		else
			echo "<font color=\"#20578d\">Successfully Created Config.php</font>";
		}	

?>
	
	<? if (isset($_POST['check']) && $_POST['check'] == " Write Config File ") { ?><input type='submit' name='action' value=' Installation Complete ' style='font-family: verdana, tahoma, arial; font-size: 10px'> <? }else { ?><input type='submit' name='check' value=' Write Config File ' style='font-family: verdana, tahoma, arial; font-size: 10px'><? } ?>
		
		</td>
    </tr>
  </form>
</table>

<? } else { ?>

<table cellspacing='1' class='install_main' width='100%'>
	<tr>
<? 
	echo "<td colspan=\"2\" class=\"install_top\"><b>DoD Block Exploits Web Front-End v".$version." Setup - Step 1: Database Infomation</b></td>"
?>
	</tr>
	<tr bgcolor="#7a96c8">
		<td colspan='2' class='install_1'><br>

		This is where you enter Database information.
<br><br>	The Database server can be entered either as a hostname or IP-address.
<br>		Make sure that the user has the CREATE privilege.
<br>		Also make sure that this user has the access to connect from this host.		
		<br><br></td>
	</tr>

	<form name="section" method="post" action="<?=$_SERVER['PHP_SELF'] ?>">
	<tr bgcolor="#7a96c8">
		<td width='30%' class='install_1'>Database Server</td>
		<td width='70%' class='install_1'><input type="text" name="db_host" value="<?= isset($_POST['db_host']) ? $_POST['db_host'] : "localhost" ?>" style="font-family: verdana, tahoma, arial; font-size: 10px; width: 150px"></td>
	</tr>
		<tr bgcolor="#7a96c8">
		<td width='30%' class='install_1'>Database Port</td>
		<td width='70%' class='install_1'><input type="text" name="db_port" value="<?= isset($_POST['db_port']) ? $_POST['db_port'] : "3306" ?>" style="font-family: verdana, tahoma, arial; font-size: 10px; width: 150px"></td>
	</tr>
	<tr bgcolor="#7a96c8">
		<td width='100%' class='install_2' colspan='2'> -</td>
	</tr>
	<tr bgcolor="#7a96c8">
		<td width='30%' class='install_1'>DB Name</td>
		<td width='70%' class='install_1'><input type="text" name="db_name" value="<?=isset($_POST['db_name']) ? $_POST['db_name'] : "" ?>" style="font-family: verdana, tahoma, arial; font-size: 10px; width: 150px"></td>
	</tr>
	<tr bgcolor="#7a96c8">
		<td width='30%' class='install_1'>DB Username</td>
		<td width='70%' class='install_1'><input type="text" name="db_user" value="<?=isset($_POST['db_user']) ? $_POST['db_user'] : "" ?>" style="font-family: verdana, tahoma, arial; font-size: 10px; width: 150px"></td>
	</tr>
	<tr bgcolor="#7a96c8">
		<td width='30%' class='install_1'>DB Password</td>
		<td width='70%' class='install_1'><input type="text" name="db_pass" value="<?=isset($_POST['db_pass']) ? $_POST['db_pass'] : "" ?>" style="font-family: verdana, tahoma, arial; font-size: 10px; width: 150px"></td>
	</tr>
	<tr bgcolor="#7a96c8">
		<td class='install_1' colspan='2' align='right'>

<?

	if ((isset($_POST['check'])) && ($_POST['check'] == " Check DB Connection ")) {
		if ($dblogin == 0) {
			echo "<font color=\"#fffc00\">Please fill in all required fields.</font>";
		} else if ($dblogin == 1) {
			echo "<font color=\"#fffc00\">Can't connect to server. Please check your connection details and try again.</font>";
		} else if($dblogin == 2) {
			echo "<font color=\"#fffc00\">Database '".$_POST['db_name']."' is not accessible. Please create database '".$_POST['db_name']."' first, and try again.</font>";
		} else {
			echo "<font color=\"#20578d\">Database Connection Successful.</font>";
		}
	}

?>

<? if ((!isset($dblogin)) || ($dblogin != 3)) { echo "<input type='submit' name='check' value=' Check DB Connection ' style='font-family: verdana, tahoma, arial; font-size: 10px'>"; } if ((isset($_POST['check']) && ($_POST['check'] == " Check DB Connection ")) && (isset($dblogin) && ($dblogin == 3))) { echo "<input type='submit' name='action' value=' Continue To Step 2 ' style='font-family: verdana, tahoma, arial; font-size: 10px'>"; } ?></td>
	</tr>
	</form>
</table>

<? } ?>

		</td>
	</tr>
</table>

<br><br>
	<a class='custom1' href=\"http://www.dodplugins.net/forums/showthread.php?t=1230\">DoD BlockExploits</a><br>
	<a class='custom1' href='mailto:diamond-optic@avamods.com'>diamond-optic</a><br><br>
<? 
	echo "Web Front-End Version ".$version."<br>";
?>
	2008
<br><br>

</td></tr>
</table>

</center>
</body>

</html>